package com.shawn.baseappbygroovydsl.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.shawn.baseappbygroovydsl.R
import java.util.LinkedList
import java.util.Random

/**
 * @author Secret
 * @since 2024/6/26
 */
class AdapterViewFlipper(val context: Context): BaseAdapter() {

    private val random = Random()

    private val amount = LinkedList<String>().apply {
        add("0.38")
        add("0.58")
        add("1.0")
        add("5.0")
        add("10.0")
        add("50.0")
    }

    private val imgRes = LinkedList<Int>().apply {
        add(R.mipmap.a1)
        add(R.mipmap.a2)
        add(R.mipmap.a3)
        add(R.mipmap.a4)
        add(R.mipmap.a5)
        add(R.mipmap.a6)
    }

    override fun getCount(): Int {
        return imgRes.size
    }

    override fun getItem(position: Int): Any {
        return imgRes[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val itemView:View?
        val viewHolder: ViewHolder?
        if (convertView == null) {
            itemView = LayoutInflater.from(context).inflate(R.layout.adapter_view_flipper, parent,false);
            viewHolder = ViewHolder(itemView)
            itemView.tag = viewHolder
        } else {
            itemView = convertView
            viewHolder = convertView.tag as ViewHolder
        }
        randomView(viewHolder,position)
        return itemView!!
    }

    private fun randomView(vh: ViewHolder, position: Int){
        vh.tvUsr.text = "用户******%s提现了".format(random.nextInt(10))
        vh.tvAmount.text = amount[random.nextInt(amount.size)]
        Glide.with(context)
            .load(imgRes[random.nextInt(imgRes.size)])
            .into(vh.iv)

    }

    private class ViewHolder(itemView:View){
        var iv:ImageView = itemView.findViewById(R.id.siv_4_pre)
        var tvUsr:TextView = itemView.findViewById(R.id.tv_random_usr_info_pre)
        var tvAmount:TextView = itemView.findViewById(R.id.tv_amount)
    }

}